﻿
namespace TinyMceWrapper.Web.UI.WebControls
{

    #region [ Imports ]

    using System;
    using System.ComponentModel;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    #endregion

    [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Mce"), ToolboxData("<{0}:TinyMceValidator runat=\"server\" ErrorMessage=\"TinyMceValidator\" />")]
    public class TinyMceValidator : BaseValidator
    {

        #region [ Public Virtual Properties ]

        [Bindable(false), Localizable(false), Themeable(false), Category("Behavior"), DefaultValue("")]
        public virtual string InitialValue
        {
            get { return ViewState.GetValue("InitialValue", string.Empty); }
            set { ViewState.SetValue("InitialValue", value); }
        }

        [Bindable(false), Localizable(false), Themeable(false), Category("Behavior"), DefaultValue(false)]
        public virtual bool IsRequired
        {
            get { return ViewState.GetValue("IsRequired", false); }
            set { ViewState.SetValue("IsRequired", value); }
        }

        [Bindable(false), Localizable(false), Themeable(false), Category("Behavior"), DefaultValue(0)]
        public virtual int MinimumLength
        {
            get { return ViewState.GetValue("MinimumLength", 0); }
            set { ViewState.SetValue("MinimumLength", value); }
        }

        [Bindable(false), Localizable(false), Themeable(false), Category("Behavior"), DefaultValue(0)]
        public virtual int MaximumLength
        {
            get { return ViewState.GetValue("MaximumLength", 0); }
            set { ViewState.SetValue("MaximumLength", value); }
        }

        #endregion

        #region [ Override Members ]

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (base.RenderUplevel)
                Page.ClientScript.RegisterClientScriptResource(typeof(TinyMceValidator), "TinyMceValidator.js");
        }

        [SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);

            if (base.RenderUplevel)
            {
                string clientId = this.ClientID;
                Page.ClientScript.RegisterExpandoAttribute(clientId, "initialvalue", this.InitialValue.Trim());
                Page.ClientScript.RegisterExpandoAttribute(clientId, "isrequired", this.IsRequired.ToString().ToLowerInvariant());
                Page.ClientScript.RegisterExpandoAttribute(clientId, "maximumlength", this.MaximumLength.ToString(CultureInfo.InvariantCulture));
                Page.ClientScript.RegisterExpandoAttribute(clientId, "minimumlength", this.MinimumLength.ToString(CultureInfo.InvariantCulture));
                Page.ClientScript.RegisterExpandoAttribute(clientId, "evaluationfunction", "TinyMceValidatorEvalulateIsValid", false);
            }
        }

        protected override bool EvaluateIsValid()
        {
            string value = base.GetControlValidationValue(this.ControlToValidate);
            int min = this.MinimumLength;
            int max = this.MaximumLength;

            if (this.IsRequired && value.Equals(this.InitialValue.Trim()))
                return false;

            if ((min > 0 && value.Length < min) || (max > 0 && value.Length > max))
                return false;

            return true;
        }

        #endregion

    }

}
